package com.ipay.controller;

import com.ipay.entity.SysUserEntity;
import com.ipay.redis.JedisExecutor;
import com.ipay.redis.JedisKeyPrefix;
import com.ipay.service.SysUserService;
import com.ipay.utils.*;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;

@Transactional
@Controller
public class SysLoginValidateController{
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private JedisExecutor jedisExe;

    /**
     * 后台登录发送短信
     * @param username 用户名
     * @param captcha  验证码
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/sys/sendCode", method = RequestMethod.POST)
    public R getActivateValidCode( @RequestParam String mobile, HttpServletRequest request) throws IOException {
    	String username = (String) request.getSession().getAttribute("username");
    	SysUserEntity sysUser = sysUserService.queryByUserName(username);
    	if(sysUser == null){
    		return R.error("帐号不存在");
    	}
    	logger.info("-------------手机号码：" + mobile);
        if(!mobile.equals(sysUser.getMobile())){
        	return R.error("用户名与手机号码不匹配！");
        }
        
        String mobile_code = RandomStringUtils.randomNumeric(6);
        logger.info("mobile_code验证码==" + mobile_code);
        String tpl_id = "37654";//短信模版编号
        String content = URLEncoder.encode("#code#=" + mobile_code + "&#company#=道合聚盈", "utf-8");
        String result = HttpUtils.getValidCode(mobile, tpl_id, content);
        JSONObject object = JSONObject.fromObject(result);
        logger.info("发送短信验证码返回结果：" + result);
        if (object != null && object.getInt("error_code") == 0) { // 发送成功
            jedisExe.setValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_MOBILE, mobile,mobile_code, 60);//验证码
            //格式化手机号码137****1234
            mobile = phoneNumberFormat(sysUser.getMobile());
            return R.ok("验证码发送成功，请注意查收").put("mobile", mobile);
        } else {
            logger.info("====:"+object);
            return R.error("验证码发送失败");
        }
    }
    
    /**
     * 手机号码格式化微182****5716
     * 
     * @param phone
     * @return
     */
    public static String phoneNumberFormat(String phone) {
        String reStr = phone.substring(phone.length() - 4, phone.length());
        String preStr = phone.substring(0, phone.length() - 8);
        StringBuilder sb = new StringBuilder();
        sb.append(preStr).append("****").append(reStr);
        return sb.toString();
    }
    
    
}
